//
//  DDroppingViewPresenter.swift
//  Drop
//
//  Created by hushaohua on 2017/12/21.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class DDroppingViewPresenter: NSObject,HTimeObserver {

    var beginDate:Date?;
    var view:DDroppingView?;
    
    var requestFinishedClosure:(() -> Void)?;
    
    func startDropping() -> Void{
        if (!HTimeManager.sharedManager().isObserver(self)){
            HTimeManager.sharedManager().addObserver(self);
        }
        if (self.view != nil){
            self.view!.dropView.play();
        }
    }
    
    func stopDropping() -> Void {
        HTimeManager.sharedManager().removeObserver(self);
        self.beginDate = nil;
        if (self.view != nil){
            self.view!.dropView.stop();
        }
    }
    
    func startDroppingView() -> Void{
        if (self.view != nil){
            self.view!.dropView.play();
        }
    }
    
    func stopDroppingView() -> Void{
        if (self.view != nil){
            self.view!.dropView.stop();
        }
    }
    
    var intervalChangedClosure:((Int, String) -> Void)?;
    func intervalScheduled(_ timestampFromReference: TimeInterval) {
        var text:String = "";
        if (self.beginDate == nil){
            text = "00:00";
        }else{
            let interval:Int = Int(Date().timeIntervalSince(self.beginDate!));
            if (interval < 60){
                text = String(format: "00:%02d", Int(interval));
            }else if (interval < 3600){
                text = String(format: "%02d:%02d", interval / 60, interval % 60);
            }else{
                text = String(format: "%d:%02d:%02d", interval / 3600, (interval / 60) % 60, interval % 60);
            }
            if (self.intervalChangedClosure != nil){
                self.intervalChangedClosure!(interval, text);
            }
            self.view?.dropView.play();
            if (interval % 2 == 0){
            }
        }
        
        self.view?.intervalLabel.text = text;
    }
}
